package com.yoomiito.app.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import androidx.recyclerview.widget.GridLayoutManager
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.GasFormat
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.ui.yijiayou.GasFormatAdapter
import com.yoomiito.app.utils.DensityUtil

class SortTabItemView @JvmOverloads constructor(context: Context, var attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {

    private val mTv = TextView(context)
    private val mIndicatorIv = ImageView(context)
    lateinit var data: List<ViewType<GasFormat>>
    lateinit var parentView: View
    lateinit var shadeView: View
    var mText = ""
        set(value) {
            field = value
            if (mTv != null) {
                mTv.text = mText
            }
        }
    var mTextSize = 0
        set(value) {
            field = DensityUtil.dp2px(value.toFloat())
            mTv?.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize.toFloat())
        }
    var mTextColor = Color.parseColor("#FF5CB331")
        set(value) {
            field = value
            mTv?.setTextColor(mTextColor)
        }
    var mSelectIndicatorRes = R.drawable.icon_down_1
        set(value) {
            field = value
            mIndicatorIv?.setImageResource(mSelectIndicatorRes)
        }
    var mDefaultIndicatorRes = R.drawable.icon_up_1
        set(value) {
            field = value
            mIndicatorIv?.setImageResource(mDefaultIndicatorRes)
        }
    var clickResult = false

    var onFormatClickListener: (GasFormat) -> Unit = {}
    init {
        initAttr()
        initView()
    }

    private fun initAttr() {
        val ta: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.SortTabItemView)
        mText = ta.getString(R.styleable.SortTabItemView_tv_text).toString()
        mTextColor = ta.getColor(R.styleable.SortTabItemView_tv_textColor, mTextColor)
        mTextSize = ta.getDimensionPixelSize(R.styleable.SortTabItemView_tv_textSize, 14)
        mDefaultIndicatorRes = ta.getResourceId(R.styleable.SortTabItemView_default_indicator, R.drawable.icon_down_1)
        mSelectIndicatorRes = ta.getResourceId(R.styleable.SortTabItemView_select_indicator, R.drawable.icon_up_1)
        ta.recycle()
    }

    fun resetInit(value:String){
        mTv.text = value
    }

    private fun initView() {
        val left = DensityUtil.dp2px(12f)
        val top = DensityUtil.dp2px(8f)
        setPadding(left, top, left, top)

        orientation = HORIZONTAL
        gravity = Gravity.CENTER
        mTv.setTextColor(mTextColor)
        mTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize.toFloat())
        mTv.text = mText
        val tvLp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        addView(mTv, tvLp)

        mIndicatorIv.setImageResource(mDefaultIndicatorRes)
        val ivLp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        ivLp.leftMargin = DensityUtil.dp2px(6f)
        addView(mIndicatorIv, ivLp)

        setOnClickListener {
            if (parentView != null && shadeView != null && data != null) {
                showFormatDialog()
            }
            clickResult = !clickResult
            setBackgroundResource(if (clickResult) R.drawable.bg_color_3bb033_15 else R.color.color_F4F4F4)
            mIndicatorIv.setImageResource(if (clickResult) mSelectIndicatorRes else mDefaultIndicatorRes)
        }
    }

    private fun handleResult() {
        clickResult = false
        setBackgroundResource(if (clickResult) R.drawable.bg_color_3bb033_15 else R.color.color_F4F4F4)
        mIndicatorIv.setImageResource(if (clickResult) mSelectIndicatorRes else mDefaultIndicatorRes)
    }

    private fun showFormatDialog() {
        val grid = androidx.recyclerview.widget.GridLayoutManager(context, 4)
        grid.spanSizeLookup = object : androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(p0: Int): Int {
                return if (data[p0].viewType == GasFormatAdapter.Subtitle) 4 else 1
            }
        }
        val adapter = GasFormatAdapter(context, data)
        val p = FormatPopupWindow(context, parentView, grid, adapter)
        adapter.onClickListener = {
            mTv.text = it.name
            onFormatClickListener(it)
            p.dismiss()
        }

        //添加背景色
        shadeView.visibility = View.VISIBLE
        p.setOnDismissListener {
            shadeView.visibility = View.GONE
            handleResult()
        }
    }
}